#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.IO;
using System.Security.AccessControl;

namespace Newtonsoft.Utilities.IO.FileSystem
{
  public class File : IFile
  {
    private readonly IFileSystem _fileSystem;
    private readonly FileInfo _fileInfo;

    public File(IFileSystem fileSystem, string path)
    {
      _fileSystem = fileSystem;
      _fileInfo = new FileInfo(path);
    }

    public string Name
    {
      get { return _fileInfo.Name; }
    }

    public string FullName
    {
      get { return _fileInfo.FullName; }
    }

    public DateTime LastAccessTimeUtc
    {
      get { return _fileInfo.LastAccessTimeUtc; }
      set
      {
        if (_fileSystem.UpdateFileLastAccess)
          _fileInfo.LastAccessTimeUtc = value;
      }
    }

    public void Delete()
    {
      _fileInfo.Delete();
    }

    public bool Exists
    {
      get
      {
        _fileInfo.Refresh();
        return _fileInfo.Exists;
      }
    }

    public void CopyTo(string path)
    {
      _fileInfo.CopyTo(path, true);
    }

    public IDirectory Parent
    {
      get { return _fileSystem.GetDirectory(_fileInfo.DirectoryName); }
    }

    public Stream Open(FileMode mode)
    {
      LastAccessTimeUtc = _fileSystem.Clock.Now.UtcDateTime;

      return new FileStream(FullName, mode);
    }

    public Stream Open(FileMode mode, FileAccess access, FileShare share)
    {
      LastAccessTimeUtc = _fileSystem.Clock.Now.UtcDateTime;

      return new FileStream(FullName, mode, access, share);
    }

    public FileSecurity GetAccessControl()
    {
      return _fileInfo.GetAccessControl();
    }

    public void SetAccessControl(FileSecurity fileSecurity)
    {
      _fileInfo.SetAccessControl(fileSecurity);
    }

    public Stream Create()
    {
      return System.IO.File.Create(FullName);
    }
  }
}